package com.ruoyi.project.system.user.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.project.system.menu.domain.LevelOneMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.menu.domain.Menu;
import com.ruoyi.project.system.menu.service.IMenuService;
import com.ruoyi.project.system.user.domain.User;

/**
 * 首页 业务处理
 * 
 * @author ruoyi
 */
@Controller
public class IndexController extends BaseController
{
    @Autowired
    private IMenuService menuService;

    @Autowired
    private RuoYiConfig ruoYiConfig;

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        User user = getUser();
        // 根据用户id取出菜单
        List<Menu> menus = menuService.selectMenusByUserId(user.getUserId());
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("copyrightYear", ruoYiConfig.getCopyrightYear());
        return "index";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap)
    {

        mmap.put("version", ruoYiConfig.getVersion());

        Menu menu = new Menu();

        List<Menu> userHaveMenus = menuService.selectSpecificHomepageImg(getUser().getUserId().toString());//当前用户拥有权限且有图片的菜单
        ArrayList<LevelOneMenu> levelOneMenus = new ArrayList<LevelOneMenu>();//一级菜单集合
        LevelOneMenu levelOneMenu = new LevelOneMenu();//一级菜单
        ArrayList<Menu> menus = new ArrayList<Menu>();//二机菜单集合


        for (int i = 0;i<userHaveMenus.size();i++){
            if(i==0){
                levelOneMenu = new LevelOneMenu();//一级菜单
                menus = new ArrayList<Menu>();//二机菜单集合
                menus.add(userHaveMenus.get(i));
            }else if (i==userHaveMenus.size()-1){
                menus.add(userHaveMenus.get(i));
                levelOneMenu.setChild(menus);
                levelOneMenus.add(levelOneMenu);
            }
            else {
                if(!userHaveMenus.get(i).getParentId().equals(userHaveMenus.get(i-1).getParentId())){//此菜单和上个菜单父菜单不相同时
                    levelOneMenu.setChild(menus);
                    levelOneMenus.add(levelOneMenu);

                    levelOneMenu = new LevelOneMenu();//一级菜单
                    menus = new ArrayList<Menu>();//二机菜单集合

                    menus.add(userHaveMenus.get(i));
                }else{
                    menus.add(userHaveMenus.get(i));
                }

            }
        }
        //主页显示的特定图片链接
        mmap.put("homepages",levelOneMenus);
        return "main";
    }

}
